package com.Smart.smartscals.CustomView;

import java.util.Timer;
import java.util.TooManyListenersException;

import com.Smart.smartscals.Entity.EntityCircleView;
import com.Smart.smartscals.Utils.CountDownTimer;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Paint.FontMetricsInt;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

@SuppressLint("NewApi")
public class CustomCircleView extends View {
	// 本环形VIew的实例
	private CustomCircleView mCircleView;
	// 绘笔
	private Canvas mCanvas;
	private Paint mPaint;
	// progressBar的宽度
	private int widthCircle;
	private int heightCircle;
	// 半径
	private int mRadios;
	private EntityCircleView entityCircleView;
	// 绘制的Progress的方块
	private RectF mF;
	// 绘制的角度
	private int tmpAnger;
	// 一个倒计时是画progressBar,一个ProgressBar是获取延时的
	private CountDownTimer mCountDownTimer;

	public int getTmpAnger() {
		return tmpAnger;
	}

	/**
	 * 开始动画展示
	 */
	public void statAniMation() {
		if (mCountDownTimer == null) {
			mCountDownTimer = new CountDownTimer() {

				@Override
				public void startMyTask() {
					setTmpAnger((120 - this.getCurrentCountTime()) * 3);
					if (this.getCurrentCountTime() == 0) {
						setTmpAnger(0);
						// 开始动画
						if (mCircleView != null)
							ObjectAnimator//
									.ofFloat(mCircleView, "rotationX", 0.0F,
											360.0F)//
									.setDuration(500)//
									.start();
						// 停止定时器
						this.stopCountDown();
					}
					Log.v("msg", "--->invalidate--------1");
					invalidate();
				}
			};
		}

		if (mCountDownTimer.getCurrentCountTime() != 0) {
			Log.v("msg", "--->invalidate***************1");
			mCountDownTimer.stopCountDown();
		} else {
			Log.v("msg", "--->invalidate*******************2");
			mCountDownTimer.startCountDown(120, 5);
		}
	}

	public CustomCircleView(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	public void initParam(EntityCircleView mCircleViewEntityCircleView) {

		Log.v("msg", "---->initParam----》1");

		this.entityCircleView = mCircleViewEntityCircleView;

	}

	@Override
	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);

		mCanvas = canvas;
		initDrawParam();
		clear();
		drawViews();

	}

	/*
	 * 初始化绘制的参数
	 */
	private void initDrawParam() {

		if (mRadios != 0)
			return;
		this.mCircleView = this;
		mRadios = mCircleView.getWidth() / 2;
		widthCircle = mRadios / 13;
		heightCircle = mCircleView.getHeight();
		Log.v("msg", "--->widTh:" + mRadios);
		mPaint = new Paint();
		mF = new RectF(widthCircle, widthCircle, 2 * mRadios - widthCircle, 2
				* mRadios - widthCircle);
		mPaint.setStrokeWidth(widthCircle);
	}

	public void clear() {
		Paint paint = new Paint();
		paint.setXfermode(new PorterDuffXfermode(Mode.DST));
		mCanvas.drawPaint(paint);
	}

	private void drawViews() {
		// 绘制外环
		mPaint.setARGB(60, 127, 255, 212);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(widthCircle);
		mPaint.setAntiAlias(true);
		mCanvas.drawCircle(mRadios, mRadios, mRadios - widthCircle, mPaint);
		// 绘制内环；
		mPaint.setColor(Color.WHITE);
		mCanvas.drawCircle(mRadios, mRadios, mRadios - 2 * widthCircle, mPaint);
		mPaint.setStyle(Paint.Style.FILL);
		drawText(mCanvas);

		drawProgress();

	}

	private void drawProgress() {
		mPaint.setStrokeWidth(widthCircle / 2);
		mPaint.setStyle(Paint.Style.STROKE);

		mCanvas.drawArc(mF, 0, tmpAnger, false, mPaint);

	}

	private void drawText(Canvas canvas) {

		RectF targetRect0 = new RectF(0, 0, 2 * mRadios, 2 * mRadios);

		RectF targetRect1 = new RectF(0, 0, 2 * mRadios, mRadios);
		RectF targetRect2 = new RectF(0, mRadios, 2 * mRadios, 2 * mRadios);

		RectF targetRect3 = new RectF(0, 2 * mRadios, 2 * mRadios, heightCircle);
		mPaint.setStrokeWidth(1);
		mPaint.setTextSize(2 * mRadios / 5);

		drawCeninView(targetRect0, canvas, mPaint,
				entityCircleView.getTvCenter());
		mPaint.setTextSize(2 * mRadios / 10);
		drawCeninView(targetRect1, canvas, mPaint, entityCircleView.getTvUp());
		mPaint.setTextSize(2 * mRadios / 8);
		drawCeninView(targetRect2, canvas, mPaint, entityCircleView.getTvDown());

		if (entityCircleView.getTvBelowCircle() == null)
			return;
		mPaint.setTextSize(2 * mRadios / 7);
		drawCeninView(targetRect3, canvas, mPaint,
				entityCircleView.getTvBelowCircle());

	}

	private void drawCeninView(RectF targetRect0, Canvas canvas, Paint paint,
			String tvStr) {
		FontMetricsInt fontMetrics = paint.getFontMetricsInt();
		int baseline = (int) (targetRect0.top
				+ (targetRect0.bottom - targetRect0.top - fontMetrics.bottom + fontMetrics.top)
				/ 2 - fontMetrics.top);
		paint.setTextAlign(Paint.Align.CENTER);
		canvas.drawText(tvStr, targetRect0.centerX(), baseline, paint);
	}

	public void setTmpAnger(int tmpAnger) {
		// TODO Auto-generated method stub
		this.tmpAnger = tmpAnger;
	}
}
